package com.imodia.checkoo.webtag;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.hsq.commons.util.BeanFactory;
import com.hsq.commons.util.tree.TreeNodeBean;
import com.imodia.checkoo.bean.CmsPageBean;
import com.imodia.checkoo.service.CmsPageService;

public class NavigationTag extends RequestContextAwareTag {

	private static final long serialVersionUID = -1764906844122921796L;
	private TldParam tldParam;

	public TldParam getTldParam() {
		return tldParam;
	}

	public void setTldParam(TldParam tldParam) {
		this.tldParam = tldParam;
	}

	@Override
	protected int doStartTagInternal() {
		CmsPageService cmsPageService = (CmsPageService) BeanFactory.getBean("cmsPageService");
		List<CmsPageBean> list = cmsPageService.list(tldParam.getSiteId(),-1,2,1);
		List<TreeNodeBean> treeList = cmsPageService.covertTreePage(list,0);
		pageContext.setAttribute("NavigationTag", treeList);
		if (tldParam.getHtmls() != null && tldParam.getHtmls().trim().length() > 0) {
			Writer out = pageContext.getOut();
			try {
				out.write(TldUtil.convertPage(list, tldParam).toString());
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			out = null;
		}
		return EVAL_BODY_INCLUDE;
	}

}
